package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.common.exception.MyException;
import com.gxa.p2p.pojo.Version;
import com.gxa.p2p.service.VersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 罗雪锋
 */
@CrossOrigin
@RestController
@RequestMapping("/version")
@WebAppConfiguration
//@Transactional
@Api(value = "版本接口")
public class VersionController {
    @Autowired
    private VersionService versionService=null;
    @Autowired
    private UploadController uploadController=null;

    /**
     * 罗雪锋
     * @param vstate
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/findVersionByPage")
    @ApiOperation("查询所有版本")
    @ApiImplicitParams({@ApiImplicitParam(name = "vstate",value = "版本状态",required =false,dataType = "Boolean"),@ApiImplicitParam(name = "page",value = "页数",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页多少数据",required = false)}
    )
    public Map findVersionByPage(
             Boolean vstate,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Version> maps = versionService.selectAll(vstate, page, size);
        List<Version> versions = versionService.selectAllData();
        int size1 = versions.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }

    /**
     * 罗雪锋
     * @param version
     * @return
     */
    @PostMapping("/addVersion")
    @ApiOperation("添加版本")
    public Map addVersion(Version version) throws MyException, IOException {
        versionService.add(version);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
       return jsonResult.getDataMap();
    }

    /**
     * 罗雪锋
     * @param vid
     * @param vstate
     * @return
     */
    @PostMapping("/updateVersionState")
    @ApiOperation("修改版本状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "vid",value = "版本id",required = true),@ApiImplicitParam(name = "vstate",value = "版本状态",required = true)})
    public Map updateVersionState(@RequestParam("vid") Long vid,@RequestParam("vstate") Boolean vstate) throws MyException {
        versionService.update(vid, vstate);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 罗雪锋，删除版本操作
     * @param vid
     * @return
     * @throws MyException
     */
    @PostMapping("/delete")
    @ApiOperation("删除/停用版本")
    @ApiImplicitParams({@ApiImplicitParam(name = "vid",value = "版本id",required = true)})
    public Map delete(@RequestParam("vid") String vid) throws MyException {
        Long s = Long.valueOf(vid);
        versionService.deleteById(s);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }



}
